package com.gbr.client.view;

import java.io.File;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.MenuItemBuilder;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import com.gbr.client.presentation.IDesktopPresentation;

public class MenuBuilder {
    private IDesktopPresentation presentation = null;
    private Stage                parent;

    public MenuBuilder(IDesktopPresentation prez, Stage stage) {
        presentation = prez;
        parent = stage;
    }

    public MenuBar build() {
        MenuBar menuBar = new MenuBar();
        menuBar.getMenus().addAll(buildFileMenu(), buildEditMenu(), buildRecordMenu());

        return menuBar;
    }

    private Menu buildFileMenu() {
        Menu menu = new Menu("File");

        final MenuItem fileExport = MenuItemBuilder.create().text("Export as GIF").onAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                File file = getFile();
                if (file != null) {
                    presentation.export(file.getAbsolutePath());
                }
            }
        }).accelerator(new KeyCodeCombination(KeyCode.A, KeyCombination.ALT_ANY)).build();
        menu.getItems().add(fileExport);

        final MenuItem fileExit = MenuItemBuilder.create().text("Exit").onAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                presentation.shutdown();
            }
        }).accelerator(new KeyCodeCombination(KeyCode.A, KeyCombination.CONTROL_DOWN)).build();
        menu.getItems().add(fileExit);

        return menu;
    }

    private Menu buildEditMenu() {
        Menu menu = new Menu("Edit");
        final MenuItem itemSelectRecordingArea = MenuItemBuilder.create().text("Select Recording Area").onAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                presentation.selectRecordingArea();
            }
        }).accelerator(new KeyCodeCombination(KeyCode.A, KeyCombination.CONTROL_DOWN)).build();
        menu.getItems().add(itemSelectRecordingArea);

        return menu;
    }

    private Menu buildRecordMenu() {
        Menu menu = new Menu("Record");
        final MenuItem fileRecord = MenuItemBuilder.create().text("Start").onAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                presentation.startRecording();
            }
        }).accelerator(new KeyCodeCombination(KeyCode.A, KeyCombination.CONTROL_DOWN)).build();
        menu.getItems().add(fileRecord);

        final MenuItem fileStop = MenuItemBuilder.create().text("Stop").onAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                presentation.stopRecording();
            }
        }).accelerator(new KeyCodeCombination(KeyCode.A, KeyCombination.CONTROL_DOWN)).build();
        menu.getItems().add(fileStop);

        return menu;
    }

    private File getFile() {
        File file = null;
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Export Animated GIF");
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
        fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("All Images", "*.*"), new FileChooser.ExtensionFilter("GIF", "*.gif"));
        file = fileChooser.showSaveDialog(parent);

        return file;
    }
}
